require "#{RAILS_ROOT}/app/classes/cat/ability_estimator"
require "#{RAILS_ROOT}/app/classes/cat/item_selector"
require "#{RAILS_ROOT}/app/classes/cat/cat_stopper"

#* cat_ruby.rb
#* Copyright (C) 2010  Embian Inc.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or 
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with This program.  If not, see <http://www.gnu.org/licenses/>.
#  
#* Perform CAT
#* Author: KyoungMo Yang(E-mail : mo@embian.com)
#* Modified: 2010-11-30
#* References: N/A
class CATRuby
  attr_accessor :profile
  
  #* Functionailities
  # * Constructor for CATRuby class.
  #* Parameters 
  # * [profile] : an instance of UserProfile class
  #* Return
  # * An instance of CATExam class
  #* Note
  # * N/A
  def initialize(profile)
   @profile = profile
   @estimator = AbilityEstimator.new(@profile)
   @selector = ItemSelector.new(@profile)
   @stopper = CATStopper.new(@profile)
  end
  
  #* Functionailities
  # * Returns an item selected by "Item Selection Rule" or "Starting Point Rule"
  #* Parameters 
  # * [item] : CATItem or CATItem#iid
  # * [answer] : examinee's answer about the treated item
  #* Return
  # * [response] : response(CATExam::MISSING_RESPONSE, CATExam::CORRECT_RESPONSE, CATExam::INCORRECT_RESPONSE)
  #* Note
  # * N/A
  def select_item
    cat_item = nil
    if @profile.exam.items.empty?
      cat_item = @selector.get_starting_point
    else
      cat_item = @selector.select_item
    end
    
    return @profile.exam.add_item(cat_item)
  end
  
  #* Functionailities
  # * Sets examinee's answer for given item
  #* Parameters 
  # * [item] : CATItem or CATItem#iid
  # * [answer] : examinee's answer
  #* Return
  # * [response] : response(CATExam::MISSING_RESPONSE, CATExam::CORRECT_RESPONSE, CATExam::INCORRECT_RESPONSE)
  #* Note
  # * N/A
  def response(item, answer)
    return @profile.exam.answer_to(item, answer)
  end
  
  #* Functionailities
  # * Updates examinee's ability based on all prior responses
  #* Parameters 
  # * N/A
  #* Return
  # * [theta] : ability
  #* Note
  # * N/A
  def update_ability
    return @estimator.estimates
  end
  
  #* Functionailities
  # * Returns true if satisfies given Stopping Rule
  #* Parameters 
  # * N/A
  #* Return
  # * [result] : Returns true if satisfies given Stopping Rule, otherwize returns false.
  #* Note
  # * N/A
  def can_stop
    return @stopper.can_stop
  end
end